﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AttentenceCheckSystem
{
    class PlayerRecord
    {
        public PlayerRecord(string strPlayerName)
        {
            PlayerName = strPlayerName;
            Late_minutes = 0;
        }

        private Dictionary<DateTime, RecordItem> _records = new Dictionary<DateTime, RecordItem>();

        private Dictionary<RecordItem.MEMO, int> _memo = new Dictionary<RecordItem.MEMO, int>();

        public string PlayerName
        {
            get;
            private set;
        }

        public int Late_minutes
        {
            get;
            private set;
        }

        public string Memo
        {
            get{
                string str = "";
                foreach (KeyValuePair<RecordItem.MEMO, int> entry in _memo)
                {
                    switch (entry.Key)
                    {
                        case RecordItem.MEMO.MISS_BOTH:
                            str += string.Format("{0}次上下班均未检测到打卡记录;\n", entry.Value);
                            break;
                        case RecordItem.MEMO.MISS_MORNING:
                            str += string.Format("{0}次上班未检测到打卡记录;\n", entry.Value);
                            break;
                        case RecordItem.MEMO.MISS_AFTERNOON:
                            str += string.Format("{0}次下班未检测到打卡记录;\n", entry.Value);
                            break;
                        case RecordItem.MEMO.LEAVE_EARLY:
                            str += string.Format("{0}次下班早退;\n", entry.Value);
                            break;
                        case RecordItem.MEMO.LATE:
                            str += string.Format("{0}次上班迟到，累计迟到时间{1}分钟\n", entry.Value, Late_minutes);
                            break;
                    }
                }
                return str;
            }
        }

        public ICollection<RecordItem> items
        {
            get
            {
                return _records.Values;
            }
        }

        public RecordItem this[DateTime dt]
        {
            get{
                DateTime date = dt.Date;
                if (!_records.ContainsKey(date))
                    _records[dt] = new RecordItem(date);
                return _records[dt];
            }
        }

        public RecordItem Add(DateTime dt)
        {
            DateTime date = dt.Date;
            if (!_records.ContainsKey(date))
                _records[dt] = new RecordItem(date);
            return _records[dt];
        }

        public void ApplyOff(DateTime dt, bool morning, bool afternoon)
        {
            this[dt].ApplyOff(morning, afternoon);
        }


        public void PostProcess(DateTime exceptDT)
        {
            int nLate30 = 0;
            foreach (RecordItem item in _records.Values)
            {
                if (item.date == exceptDT.Date)
                    continue;
                item.PostProcess(ref nLate30);
                if (item.memo != RecordItem.MEMO.NONE)
                {
                    if (!_memo.ContainsKey(item.memo))
                        _memo[item.memo] = 1;
                    else
                        ++_memo[item.memo];

                    if (item.memo == RecordItem.MEMO.LATE)
                    {
                        Late_minutes += item.Late_minutes;
                    }
                }
            }
            _records.Remove(exceptDT.Date);
        }
    }
}
